#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

void panic(char *message) {
    perror(message);
    exit(1);
}

void print_tree(char *dirname, int level) {
    DIR *dirp;
    struct dirent *direntp;

    if((dirp = opendir(dirname)) == NULL)
        panic("opendir");

    while((direntp = readdir(dirp)) != NULL) {
        char *name = direntp->d_name;
        int type = direntp->d_type;
        if(!strcmp(name, ".") || !strcmp(name, "..") || !strcmp(name, ".git")) continue;
        int t = level * 2;
        while(t --) printf(" ");
        printf("%s\n", name);
        if(type == DT_DIR) {
            char tmp[strlen(dirname)];
            strcpy(tmp, dirname);
            strcat(tmp, "/");
            strcat(tmp, name);
            print_tree(tmp, level + 1);
        }
    }
    closedir(dirp);
}

int main(int argc, char *argv[]) {
    char *path;
    if(argc == 1) path = ".";
    else path = argv[1];
    printf("%s\n", path);
    print_tree(path, 1);
    return 0;
}
